package a51qindian.com.comprehensive.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.li.utils.KeyboardUtil;
import com.li.utils.ToastUtil;
import com.li.widget.MultipleLayout;
import com.li.widget.TopNavigationBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.base.QDBaseActivity;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import a51qindian.com.comprehensive.util.SpUtil;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/9/18 0018
 * Description: 首装分配
 */

public class FirstInstallActivity extends QDBaseActivity {


    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.layout_content)
    MultipleLayout mLayoutContent;
    @BindView(R.id.txt_shop_name)
    TextView mTxtShopName;
    @BindView(R.id.layout_shop)
    LinearLayout mLayoutShop;
    @BindView(R.id.et_address)
    TextView mEtAddress;
    @BindView(R.id.et_phone)
    TextView mEtPhone;
    @BindView(R.id.txt_staff)
    TextView mTxtStaff;
    @BindView(R.id.layout_staff)
    LinearLayout mLayoutStaff;
    @BindView(R.id.et_number)
    EditText mEtNumber;
    private String mShopId;
    private String mShopName;
    private String mStaffId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_first_install;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
    }

    public static final int REQUEST_CHOOSE_SHOP=100;
    public static final int REQUEST_CHOOSE_STAFF=101;
    @Override
    public void onFunctionTxtListener(View view) {
        super.onFunctionTxtListener(view);
        if (TextUtils.isEmpty(mShopId)) {
            showToast("请选择商铺");
            return;
        }
        if (TextUtils.isEmpty(mStaffId)) {
            showToast("请选择安装工");
            return;
        }
        if (TextUtils.isEmpty(mEtNumber.getText().toString())||mEtNumber.getText().toString().equals("0")) {
            showToast("请正确输入安装数量");
            return;
        }
        KeyboardUtil.hideKeyboard(FirstInstallActivity.this,view);
        AlertDialog.Builder builder = new AlertDialog.Builder(FirstInstallActivity.this)
                .setTitle("提示")
                .setMessage("确认分配该任务")
                .setNegativeButton("取消", null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        apply(SpUtil.getUserInfo(FirstInstallActivity.this,SpUtil.USER_ID),
                                mShopId,mShopName,mEtAddress.getText().toString(),
                                mEtPhone.getText().toString(),mStaffId,mEtNumber.getText().toString());
                    }
                });
        builder.create().show();
    }

    @OnClick({R.id.layout_shop, R.id.layout_staff})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_shop:
                Intent intentChooseShop=new Intent(FirstInstallActivity.this,ChooseShopActivity.class);
                startActivityForResult(intentChooseShop,REQUEST_CHOOSE_SHOP);
                break;
            case R.id.layout_staff:
                Intent intentChooseStaff=new Intent(FirstInstallActivity.this,ChooseStaffActivity.class);
                intentChooseStaff.putExtra("source","install");
                startActivityForResult(intentChooseStaff,REQUEST_CHOOSE_STAFF);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        switch (requestCode) {
            case REQUEST_CHOOSE_SHOP:
                mShopId = data.getStringExtra("shopId");
                mShopName = data.getStringExtra("shopName");
                String shopAddress=data.getStringExtra("shopAddress");
                String shopPhone=data.getStringExtra("shopPhone");
                mEtAddress.setText(shopAddress);
                mEtPhone.setText(shopPhone);
                mTxtShopName.setText(mShopName);
                break;
            case REQUEST_CHOOSE_STAFF:
                mStaffId = data.getStringExtra("staffId");
                String staffName=data.getStringExtra("staffName");
                mTxtStaff.setText(staffName);
                break;
        }
    }

    /**
     * 任务分配
     * @param uid   用户id
     * @param shopId   商铺id
     * @param shopName  商铺名称
     * @param address   地址
     * @param mobile    电话
     * @param staffId   员工编号
     */
    private void apply(String uid,String shopId,String shopName,String address,String mobile,String staffId,
                       String number){
        mLayoutContent.onLoadingWithContent("正在分配");
        Map<String,String> map=new HashMap<>();
        map.put("uid",uid);
        map.put("shop_id",shopId);
        map.put("shop_name",shopName);
        map.put("address",address);
        map.put("mobile",mobile);
        map.put("repair_id",staffId);
        map.put("number",number);

        ObservableHelper.createObservable(RetrofitCreator.createService().firstInstall(map))
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        mLayoutContent.onSuccess();
                        try {
                            JSONObject object=new JSONObject(s);
                            int code=object.getInt("code");
                            if (code!=1) {
                                ToastUtil.showToast(FirstInstallActivity.this,object.getString("info"));
                                return;
                            }
                            ToastUtil.showToast(FirstInstallActivity.this,"分配成功");
                            finish();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                        ToastUtil.showToast(FirstInstallActivity.this,e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
